# -*- coding: utf-8 -*-
import scrapy


class LoginSpider(scrapy.Spider):
    name = 'login'
    allowed_domains = ['www.github.com']
    start_urls = ['http://www.github.com/login']

    def parse(self, response):
        print("从start_url里拿去3个关键信息")

        authenticity_token = response.xpath("//input[@name='authenticity_token']/@value").extract()[0]
        timestamp = response.xpath("//input[@name='timestamp']/@value").extract()[0]
        timestamp_secret = response.xpath("//input[@name='timestamp_secret']/@value").extract()[0]
        data = {"commit": "Sign+in",
                "authenticity_token": authenticity_token,
                "ga_id": "1012293138.1571402591",
                "login": "952302908@qq.com",
                "password": "extreme0710",
                "webauthn-support": "supported",
                "webauthn-iuvpaa-support": "unsupported",
                "return_to": "",
                "required_field_cb05": "",
                "timestamp": timestamp,
                "timestamp_secret": timestamp_secret
                }
        print(authenticity_token,timestamp,timestamp_secret)
        # 发送请求
        login_url = 'https://github.com/session'
        yield scrapy.FormRequest(login_url,method='POST',callback=self.vertify_login,formdata=data,dont_filter=True)

    def vertify_login(self, response):
        if "yozuriha" in response.body.decode():
            print("登录成功")
